﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.MSE.Repository.Triggers
{
    public enum DBEvents
    {
        EndpointsUpdated,
        OperationsUpdated,
        RuntimeServersUpdated,
        EndpointOpAssociationUpdated,
        EndpointOpPublicationUpdated
    }
    
    public enum DBChangeType
    {
        Unknown,
        Create,
        Update,
        Delete
    }

    public class DBEventContext
    {

        private DBEvents eventField;
        private List<DBEventDataItem> dbEventDataItem;
        private DBChangeType changeType;

        public DBEventContext(DBEvents dbEvent)
            : this(dbEvent, DBChangeType.Unknown)
        {
        }
        public DBEventContext(DBEvents dbEvent, DBChangeType dbChangeType)
            : this(dbEvent, dbChangeType, new List<DBEventDataItem>())
        {
        }
        public DBEventContext(DBEvents dbEvent, List<DBEventDataItem> dbEventData)
            : this(dbEvent, DBChangeType.Unknown, dbEventData)
        {
        }
        public DBEventContext(DBEvents dbEvent, DBChangeType dbChangeType, List<DBEventDataItem> dbEventData)
        {
            eventField = dbEvent;
            changeType = dbChangeType;
            dbEventDataItem = dbEventData;
        }

        public DBEvents Event
        {
            get { return this.eventField; }
            set { this.eventField = value; }
        }
        public List<DBEventDataItem> EventData
        {
            get { return this.dbEventDataItem; }
            set { this.dbEventDataItem = value; }
        }
        public DBChangeType ChangeType
        {
            get { return this.changeType; }
            set { this.changeType = value; }
        }
        public override int GetHashCode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Enum.GetName(typeof(DBEvents), this.Event));
            sb.Append(Enum.GetName(typeof(DBChangeType), this.ChangeType));
            if (this.EventData != null && this.EventData.Count > 0)
                this.EventData.ForEach(data => sb.Append("$" + data.GetHashCode()));
            return sb.ToString().GetHashCode();
        }
    }

    public class DBEventDataItem
    {
        private Guid runtimeServerId;
        private Guid endpointId;
        private Guid operationId;

        public DBEventDataItem() : this(Guid.Empty) { }
        public DBEventDataItem(Guid runtimeServerId) : this(runtimeServerId, Guid.Empty) { }
        public DBEventDataItem(Guid runtimeServerId, Guid endpointId) : this(runtimeServerId, endpointId, Guid.Empty) { }
        public DBEventDataItem(Guid runtimeServerId, Guid endpointId, Guid operationId)
        {
            this.runtimeServerId = runtimeServerId;
            this.endpointId = endpointId;
            this.operationId = operationId;
        }

        public Guid EndpointId
        {
            get { return endpointId; }
            set { endpointId = value; }
        }
        public Guid OperationId
        {
            get { return operationId; }
            set { operationId = value; }
        }
        public Guid RuntimeServerId
        {
            get { return runtimeServerId; }
            set { runtimeServerId = value; }
        }
        public override int GetHashCode()
        {
            string temp = string.Format("{0}:{1}:{2}", this.RuntimeServerId, this.EndpointId, this.OperationId);
            return temp.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            DBEventDataItem item = obj as DBEventDataItem;
            if (item == null)
                return false;

            if (item.RuntimeServerId == this.RuntimeServerId && item.OperationId == this.OperationId && item.EndpointId == this.EndpointId)
                return true;
            
            return false;
        }
    }
}